package com.bluishoul.bean;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import my.cache.CacheManager;
import my.db.POJO;
import my.db.QueryHelper;
import my.util.ResourceUtils;

/**
 * 持久化对象的基类
 * @author liudong
 */
public abstract class Pojo extends POJO {

	protected final static transient int MODIFY_TIMEOUT_1 = 3600;	//一个小时
	protected final static transient int MODIFY_TIMEOUT_2 = 604800;//一周
	
	public final static transient byte STATUS_NORMAL = 0x01;
	public final static transient byte STATUS_POSTED = 0x02;
	
	public final static transient String CACHE_1_HOUR = "1h";
	public final static transient String CACHE_1_DATE = "1d";
	public final static transient String CACHE_10_SENDS = "10s";

	public static String SQL(String key) {
		return ResourceUtils.sql(key);
	}
		
	/**
	 * 更新某个字段值
	 * @param field
	 * @param value
	 * @return
	 */
	public boolean UpdateField(String field, Object value) {
		String sql = "UPDATE " + TableName() + " SET " + field +" = ? WHERE id=?";
		return Evict(QueryHelper.update(sql, value, getId())==1);
	}
	
	public int GetObjectCountToday(long uid) {
		String sql = "SELECT COUNT(*) FROM "+TableName()+" WHERE user=? AND create_time>?";
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return (int)QueryHelper.stat(sql, uid, cal.getTime());
	}
	
	protected int getView_count() { return 0; }
	protected void setView_count(int view_count) { }
	protected int getReply_count() { return 0; }
	protected void setReply_count(int reply_count) { }
	
	/**
	 * 更新浏览数
	 * @param id
	 * @param count
	 * @return
	 */
	public boolean UpdateViewCount(long id, int count) {
		boolean ok = QueryHelper.update("UPDATE " + TableName() + " SET view_count=view_count+? WHERE id=?", count, id)==1;
		if(ok){
			Pojo obj = CacheManager.get(getClass(), CacheRegion(), id);
			if(obj != null){
				obj.setView_count(obj.getView_count()+count);
				CacheManager.set(CacheRegion(), id, obj);
			}
		}
		return ok;
	}

	/**
	 * 更新浏览数
	 * @param datas
	 * @param update_cache
	 * @return
	 */
	public void UpdateViewCount(Map<Long, Integer> datas, boolean update_cache) {
		String sql = "UPDATE " + TableName() + " SET view_count=view_count+? WHERE id=?";
		int i=0;
		Object[][] args = new Object[datas.size()][2];
		for(long id : datas.keySet()){
			int count = datas.get(id);
			args[i][1] = id;
			args[i][0] = count;
			if(update_cache){
				Pojo obj = CacheManager.get(getClass(), CacheRegion(), id);
				if(obj != null){
					obj.setView_count(obj.getView_count()+count);
					CacheManager.set(CacheRegion(), id, obj);
				}
			}
			i++;
		}
		QueryHelper.batch(sql, args);
	}

	/**
	 * 更新评论数
	 * @param id
	 * @param count
	 * @return
	 */
	protected boolean UpdateReplyCount(long id, int count) {
		boolean ok = QueryHelper.update("UPDATE " + TableName() + " SET reply_count=? WHERE id=?", count, id)==1;
		if(ok){
			Pojo obj = CacheManager.get(getClass(), CacheRegion(), id);
			if(obj != null){
				obj.setReply_count(count);
				CacheManager.set(CacheRegion(), id, obj);
			}
		}
		return ok;
	}

	/**
	 * 返回索引存储的字段
	 * @see my.search.SearchEnabled#GetStoreFields()
	 * @return
	 */
	public String[] GetStoreFields() { return null; }

	public HashMap<String, String> GetExtendValues(){ return null; }
	
	public HashMap<String, String> GetExtendIndexValues() { return null; }
	
	/**
	 * @see my.search.SearchEnabled#GetBoost()
	 * @return
	 */
	public float GetBoost() { return 1; }

}
